﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace University.Workshop.WinApp.Hospitalization
{
    public partial class HospitalizationForm : Form
    {
        private SelectBedForm selectBed;


        public HospitalizationForm()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void brnAs_Click(object sender, EventArgs e)
        {
            selectBed = new SelectBedForm(this);
            selectBed.Show();
        }

        internal void SetSelectedBed(string bed)
        {
            txBxSelectedBed.Text = bed;
        }

        private void btnNewPatient_Click(object sender, EventArgs e)
        {
            WinApp.PatientRegister form = new PatientRegister();
            form.ShowDialog();
        }

        private void txBxPatientId_CursorChanged(object sender, EventArgs e)
        {
            
        }

        private void txBxPatientId_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txBxPatientId.Text))
                MessageBox.Show("Ingrese un ci de un paciente");
            else
            {
                if (Business.PatientManager.PatientExists(txBxPatientId.Text))
                {
                    Model.Patient patient = Business.PatientManager.ShowPatient(txBxPatientId.Text);
                    txbxPatientName.Text = patient.FirstName + " " + patient.FatherLastName;
                }
                else
                    MessageBox.Show("No existe el paciente, por favor, registrelo.");
            }
        }

        private void HospitalizationForm_Load(object sender, EventArgs e)
        {
            txBxTime.Text = DateTime.Now.ToShortTimeString();
            txBxDate.Text = DateTime.Now.ToShortDateString();

            List<Model.User> doctors = (List<Model.User>)DataAccess.UserDataAccess.GetAllUsersIdsByRole("Doctor");
            List<string>doctorsNames = new List<string>();
            foreach (var doctor in doctors)
            {
                doctorsNames.Add("Dr. " + doctor.FirstName + " " + doctor.LastName);
            }
            txBxDoctor.DataSource = doctorsNames;

            List<Model.User> nurses = (List<Model.User>)DataAccess.UserDataAccess.GetAllUsersIdsByRole("Enfermera");
            List<string> nursesNames = new List<string>();
            foreach (var nurse in nurses)
            {
                nursesNames.Add(nurse.FirstName + " " + nurse.LastName);
            }
            cmbNurse.DataSource = nursesNames;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (cmBxSpecialty.Text.Equals("Escoja una Especialidad..."))
                MessageBox.Show("Escoja una Especialidad");
            else
            {
                if (string.IsNullOrEmpty(txBxEmergencyCallTo.Text) || string.IsNullOrEmpty(txBxEmergencyPhone.Text) || string.IsNullOrEmpty(txBxEmergencyName.Text))
                    MessageBox.Show("Escriba el contacto en caso de emergencia.");
                else
                {
                    if (string.IsNullOrEmpty(txBxSelectedBed.Text))
                        MessageBox.Show("Escoja una cama.");
                    else
                    {
                        Model.Hospitalization hospitalization = new Model.Hospitalization();
                        hospitalization.PatientId = txBxPatientId.Text;
                        hospitalization.Specialty = cmBxSpecialty.Text;
                        hospitalization.DoctorId = txBxDoctor.Text;
                        hospitalization.Date = txBxDate.Text;
                        hospitalization.Time = txBxTime.Text;
                        hospitalization.EmergencyCallTo = txBxEmergencyCallTo.Text;
                        hospitalization.EmergencyName = txBxEmergencyName.Text;
                        hospitalization.EmergencyPhone = txBxEmergencyPhone.Text;
                        hospitalization.BedId = txBxSelectedBed.Text;
                        hospitalization.Alimentation = cmbAlimentation.Text;
                        hospitalization.Nurse = cmbNurse.Text;
                        hospitalization.Diet = cmbDiet.Text;
                        hospitalization.Revision = cmbRevision.Text;
                        hospitalization.BloodType = cmbTypeOfBlood.Text + cmbRH.Text;
                        hospitalization.AllergicTo = txBxAlergicTo.Text;
                        hospitalization.Observations = txBxObservations.Text;
                        hospitalization.State = "Activo";

                        if (Business.HospitalizationManager.RegisterHospitalization(hospitalization))
                        {
                            MessageBox.Show("El paciente fue registrado a internacion satisfactoriamente");
                            this.Close();
                        }
                        else
                            MessageBox.Show("No se pudo registrar la internacion");
                    }
                }
            }
        }

        private void txBxPatientId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txBxEmergencyCallTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txBxEmergencyName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txBxEmergencyPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txBxPatientId_TextChanged(object sender, EventArgs e)
        {
            txbxPatientName.Text = "";
        }
    }
}
